﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace SampleExamDB
{
    class DatingDB
    {
        public static void FillDBFromXML(string fileName) 
        {
            using (XmlReader reader = XmlReader.Create(fileName)) 
            {
                while (reader.Read())
                {
                    if ((reader.NodeType == XmlNodeType.Element)&&(reader.Name == "guys")) 
                    {
                         List<Person> newPersons=CreatePerson("guy", reader);
                         DBAccess.InsertPerson(newPersons);

                    }
                    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "girls"))
                    {
                         List<Person> newPersons=CreatePerson("girl", reader);
                         DBAccess.InsertPerson(newPersons);
                    }
                }
            }
        }

        private static List<Person> CreatePerson(string gender, XmlReader reader)
        {
            List<Person> newPersons = new List<Person>();
            newPersons.Add(new Person());
            int index = 0;
            newPersons[index].Gender = gender;
            while (reader.Read())
            {
                if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "person"))
                {
                    newPersons[index].Name = reader.GetAttribute("name");
                    newPersons[index].Haircolor = reader.GetAttribute("haircolor");
                    newPersons[index].Eyecolor = reader.GetAttribute("eyecolor");
                    newPersons[index].Hight = int.Parse(reader.GetAttribute("height"));
                    newPersons[index].Age = int.Parse(reader.GetAttribute("age"));
                }
                if((reader.NodeType ==XmlNodeType.EndElement)&&(reader.Name=="person"))
                {
                    newPersons.Add(new Person());
                    index++;
                    newPersons[index].Gender = gender;
                }
                if ((reader.NodeType == XmlNodeType.EndElement) && (reader.Name == "guys"))
                {
                    return newPersons;

                }
                if ((reader.NodeType == XmlNodeType.Element) && ((reader.Name == "girl") || (reader.Name == "guy")))
                {
                    Preference newPreference = new Preference();
                    newPreference.Gender = reader.Name;
                    reader.ReadToDescendant("height");
                    int[] startEndHight = ParseFromString(reader.ReadElementString());
                    newPreference.StartHight = startEndHight[0];
                    newPreference.EndHight = startEndHight[1];
                    reader.ReadToDescendant("haircolor");
                    newPreference.Haircolor = reader.ReadElementString();
                    reader.ReadToDescendant("eyecolor");
                    newPreference.EyeColor = reader.ReadElementString();
                    reader.ReadToDescendant("age");
                    int[] startEndAge = ParseFromString(reader.ReadElementString());
                    newPreference.StartAge = startEndAge[0];
                    newPreference.EndDate = startEndAge[1];
                    newPersons[index].Preferences.Add(newPreference);


                }
            }
            return newPersons;
        }

        private static int[] ParseFromString(string toParse)
        {
            string[] elements = toParse.Split(new char[] {'-',' ','.',','},StringSplitOptions.RemoveEmptyEntries);
            int[] asNumbers = new int[2];
            for (int i = 0; i < 2; i++)
            {
                asNumbers[i] = int.Parse(elements[i]);
            }
            return asNumbers;
        }

        static void Main(string[] args)
        {
            FillDBFromXML("bigXml.xml");
        }
    }
}
